#ifndef IMAGEWINDOW_H
#define IMAGEWINDOW_H

#include <QWidget>
#include <QTimer>
#include <QLabel>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QResizeEvent>
#include <QPainter>
#include <QVBoxLayout>
#include "halconcpp.h"

using namespace HalconCpp;

class ImageWindow : public QWidget
{
    Q_OBJECT

public:
    explicit ImageWindow(QWidget *parent = nullptr);
    ~ImageWindow();

    // Halcon窗口操作
    void InitHWindow();
    void ClearWindow();
    void ClearObj();
    void ClearImage();

    // 显示功能
    void DispImage(HObject image, bool bClearWindow = false, bool bTrans = true);
    void DispObj(HObject obj, const QString& sFormat, bool bClearWindow = false);
    void DispText(const QString& sText, const QString& sFormat, int cx, int cy, bool bClearWindow = false);

    // 设置功能
    void SetFont(int Size, const QString& Font = "mono", bool bBold = false, bool bSlant = false);
    void SetPart(int iLeft = 0, int iTop = 0, int iRight = 2592, int iBottom = 1944);
    void SetLineWidthX(int iWidth);

    // 其他功能
    void DisplayCrossCenterLine();
    void GetWindowImage(HObject &image);

    // 属性访问
    float getScale() const { return m_fScale; }
    bool getCross() const { return m_bCross; }
    void setCross(bool cross) { m_bCross = cross; }
    bool getDynamic() const { return m_bDynamic; }
    void setDynamic(bool dynamic) { m_bDynamic = dynamic; }

protected:
    // 事件处理
    void wheelEvent(QWheelEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;
    void timerEvent(QTimerEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    bool eventFilter(QObject *obj, QEvent *event) override;
    void paintEvent(QPaintEvent *event) override;

private:
    // Halcon相关成员
    HTuple m_hWindow;
    HObject m_image;
    HObject m_object;

    // 状态成员
    float m_fScale;
    bool m_bCross;
    bool m_bDynamic;
    int m_iGrayValue;
    int m_iCountRButtonUp;
    bool m_bDisableMenu;

    // Qt控件
    QLabel *m_picDraw;
    QLabel *m_statusLabel;
    QVBoxLayout *m_mainLayout;
    int m_timerId;

    // 工具函数
    void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
                      HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box);
    void set_display_font(HTuple hv_WindowHandle, HTuple hv_Size, HTuple hv_Font,
                          HTuple hv_Bold, HTuple hv_Slant);
    void gen_arrow_contour_xld(HObject *Arrow, HTuple Row1, HTuple Column1,
                               HTuple Row2, HTuple Column2, HTuple HeadLength, HTuple HeadWidth);

    // 坐标转换
    QPointF windowToImage(const QPoint& windowPoint);
    void updateStatusDisplay(const QString& text);
public:
    void setFitToWindow(bool fit) { m_bFitToWindow = fit; }
    bool getFitToWindow() const { return m_bFitToWindow; }
    void zoomToFit() { fitImageToWindow(); }

private slots:
    void onTimerTimeout();
private:

    bool m_bFitToWindow;
    double m_dAspectRatio;


    void fitImageToWindow();
    void updateImageDisplay();
};

#endif // IMAGEWINDOW_H
